<?php
class AreasController extends AppController {

	var $name = 'Areas';

	function index() {
		$this->Area->recursive = 0;
		$this->set('areas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid area', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('area', $this->Area->read(null, $id));
	}

    function procesos($area_id)                         
    {
        $ajaxx = 0;
        $headers = getallheaders();
        if (!empty($this->data)) {
            $this->Area->Proceso->create();
            if ($this->Area->Proceso->save($this->data)) {
            } else {
                $this->Session->setFlash(__('The proceso could not be saved. Please, try again.', true));
            }
            

        }
            
        if (isset($headers['x-requested-with']))
        {
            $this->layout = 'ajax';
            $ajaxx = 1;
        }
        
        $this->set(                             
                'procesos', $this->paginate(
                    $this->Area->Proceso, array("Proceso.area_id" => $area_id)
                 )
            );
        $this->set('area', $this->Area->read(null, $area_id));  
        $this->set('usuarios', $this->Area->Usuario->find('list'));
        $this->set(compact('ajaxx'));
    }

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid area', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Area->save($this->data)) {
				$this->Session->setFlash(__('The area has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The area could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Area->read(null, $id);
		}
		$empresas = $this->Area->Empresa->find('list');
		$usuarios = $this->Area->Usuario->find('list');
		$this->set(compact('empresas', 'usuarios'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for area', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Area->delete($id)) {
			$this->Session->setFlash(__('Area deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Area was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
